<?php

namespace Admin;

/**
 * Description of UnitsController
 *
 * @author dung
 */
use AdminController;
use Category;
use Config;
use DB;
use Input;
use Lang;
use Redirect;
use Sentry;
use Str;
use Validator;
use View;

class UnitsController extends AdminController
{

    //put your code here

    public function getIndex()
    {

    }

    public function getCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['unit', 'unit.create']))
            return View::make('backend/notallow');
        $objUnit = new Category();
        $data['units'] = $objUnit->getRootUnitsToArray();
        $data['unit'] = new Category();
        return View::make('backend/units/create', compact('data'));
    }

    public function postCreate()
    {
        if (!Sentry::getUser()->hasAnyAccess(['unit', 'unit.create']))
            return View::make('backend/notallow');
        $category = new Category();
        $data = Input::get('data');
        $validator = Validator::make($data['Unit'], $category->basicRules);
        if ($validator->fails())
            return Redirect::back()->withInput()->withErrors($validator);
        $data['Unit']['type'] = 'program';
        $id = $category->saveItem($data['Unit']);
        return Redirect::route('update/unit', $id)->withInput()->with('success', '');
    }

    public function getEdit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['unit', 'unit.edit']))
            return View::make('backend/notallow');
        $objUnit = new Category();
        $data['units'] = $objUnit->getRootUnitsToArray();

        if (is_null($unit = Category::find($id)))


            return View::make('backend/units/create', compact('data'));
    }

    public function getOrderPlaylist($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['unit', 'unit.edit']))
            return View::make('backend/notallow');
        if (is_null($unit = Category::find($id)))
            return Redirect::route('units')->with('error', '');
        if ($unit->type != 'program')
            return Redirect::route('units')->with('error', '');
        $units = Category::where('parent_id', '=', $id)->where('type', '=', 'program')->get();
        $data['id'] = $id;
        $data['units'] = $units;
        return View::make('backend/units/order_playlist', compact('data'));
    }

    public function postOrderPlaylist()
    {
        $pid = Input::get('pid');
        $ids = Input::get('id');
        foreach ($ids as $id) {
            $pos = Input::get('unit_' . $id);
            Category::where('id', '=', $id)->update(array('position' => $pos));
        }
        Return Redirect::route('order_playlist/unit', $pid)->withInput()->with('success', '');
    }


    public function getDeleteUnit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['unit', 'unit.delete']))
            return View::make('backend/notallow');
        if (is_null($unit = Category::find($id)))
            return Redirect::to('admin/units')->with('error', '');
        if ($unit->type != 'program')
            return Redirect::to('admin/units')->with('error', '');
        if ($unit->parent_id == 0) {
            foreach ($unit->Childs()->get() as $child)
                $child->delete();
        }
        $unit->delete();
        return Redirect::to('admin/units');
    }

    public function getRestoreUnit($id)
    {
        if (!Sentry::getUser()->hasAnyAccess(['unit', 'unit.delete']))
            return View::make('backend/notallow');
        if (is_null($unit = Category::withTrashed()->find($id)))
            return Redirect::to('admin/units')->with('error', '');
        if ($unit->type != 'program')
            return Redirect::to('admin/units')->with('error', '');
        if ($unit->parent_id != 0)
            $p = Category::withTrashed()->find($unit->parent_id)->restore();
        $unit->restore();
        return Redirect::to('admin/units');
    }
}
